import { Injectable } from "@nestjs/common";
import { PassportStrategy } from "@nestjs/passport";
import { ExtractJwt, Strategy } from "passport-jwt";
import { jwtConstants } from "../../common/tokenConstants/constants";

/**
 * 接口请求时为Authorization: 'Bearer '+token
 * 前缀为Bearer
 */

@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
  constructor() {
    super({
      // 从请求头的 Authorization 中提取 JWT
      jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),

      // JWT 签名密钥，需与 JwtModule 注册时一致
      secretOrKey: jwtConstants.secret,

      // 是否忽略过期（通常设为 false）
      ignoreExpiration: false,
    });
  }
  // 校验通过后调用，会将返回值注入到 `req.user` 中
  async validate(payload: any) {
    return {
      userId: payload.sub,
      account:payload.account
    }
  }
}